using System;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web.DynamicData;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace Altairis.FluffyCloud.WebCore.DynamicData.EntityTemplates {

    public partial class Default_EditEntityTemplate : System.Web.DynamicData.EntityTemplateUserControl {
        private MetaColumn currentColumn;

        protected void DynamicControl_Init(object sender, EventArgs e) {
            DynamicControl dynamicControl = (DynamicControl)sender;
            dynamicControl.DataField = currentColumn.Name;
        }

        protected void Label_Init(object sender, EventArgs e) {
            Label label = (Label)sender;
            label.Text = currentColumn.DisplayName + ":";
        }

        protected void Label_PreRender(object sender, EventArgs e) {
            Label label = (Label)sender;
            DynamicControl dynamicControl = (DynamicControl)label.FindControl("DynamicControl");
            FieldTemplateUserControl ftuc = dynamicControl.FieldTemplate as FieldTemplateUserControl;
            if (ftuc != null && ftuc.DataControl != null) {
                label.AssociatedControlID = ftuc.DataControl.GetUniqueIDRelativeTo(label);
            }
        }

        protected void Literal_Init(object sender, EventArgs e) {
            Literal literal = (Literal)sender;
            if (!string.IsNullOrWhiteSpace(currentColumn.Description)) {
                literal.Text = "<aside>" + currentColumn.Description + "</aside>";
            }
        }

        protected override void OnLoad(EventArgs e) {
            // Get list of groups
            var groupNames = from c in Table.GetScaffoldColumns(Mode, ContainerType)
                             group c by c.GetAttributeOrDefault<DisplayAttribute>().GroupName into g
                             select g.Key;

            foreach (var groupName in groupNames) {
                Control groupContainerControl;
                if (string.IsNullOrWhiteSpace(groupName)) {
                    // No group - put directly to EntityTemplate1
                    groupContainerControl = this.EntityTemplate1;
                }
                else {
                    // Group - add <fieldset> and <legend> elements
                    groupContainerControl = new HtmlGenericControl("fieldset");
                    groupContainerControl.Controls.Add(new HtmlGenericControl("legend") { InnerText = groupName });
                    this.EntityTemplate1.Controls.Add(groupContainerControl);
                }

                // Get columns in  this group
                var columns = from c in Table.GetScaffoldColumns(Mode, ContainerType)
                              where c.GetAttributeOrDefault<DisplayAttribute>().GroupName == groupName
                              orderby c.GetAttributeOrDefault<DisplayAttribute>().GetOrder()
                              select c;

                // Add fields
                foreach (var column in columns) {
                    currentColumn = column;
                    Control item = new DefaultEntityTemplate._NamingContainer();
                    this.EntityTemplate1.ItemTemplate.InstantiateIn(item);
                    groupContainerControl.Controls.Add(item);
                }
            }
        }
    }
}